package com.jy.horny.function.settings

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.widget.TextView
import com.google.gson.Gson
import com.jy.chat.kit.activity.RongWebviewActivity
import com.jy.horny.R
import com.jy.horny.base.BaseVMActivity
import com.jy.horny.base.HttpRequestListener
import com.jy.horny.databinding.ActivitySettingsBinding
import com.jy.horny.dialog.CustomPopup
import com.jy.horny.function.login.LoginActivity
import com.jy.horny.function.settings.viewmodel.SettingsViewModel
import com.jy.horny.model.PolicyModel
import com.jy.horny.utils.UserUtil
import com.lib.module_base.utils.SaveDeviceUtil
import com.lib.module_base.utils.StatusBarUtil

class SettingsActivity : BaseVMActivity<ActivitySettingsBinding, SettingsViewModel>(
    ActivitySettingsBinding::inflate,
    SettingsViewModel::class.java
) {
    override fun initView() {
        super.initView()
        binding.txtSettingLogOut.setOnClickListener {
            CustomPopup(this)
                .setLayoutId(R.layout.popup_log_out_account)
                .setOnClickListener(R.id.txt_cancel) { dialog, _ ->
                    run {
                        dialog.dismiss()
                    }
                }
                .setOnClickListener(R.id.txt_log_out) { dialog, _ ->
                    dialog.dismiss()
                    UserUtil.out(this)
                }.show()
        }
        // 将字符串转换为 PolicyModel 对象
        val policyModelFromJson =
            Gson().fromJson(SaveDeviceUtil.getPolicyUrl(), PolicyModel::class.java)

        binding.privacyPolicyContainer.setOnClickListener {
            val intent = Intent(this, RongWebviewActivity::class.java)
            intent.putExtra("isFull", true)
            intent.putExtra("url", policyModelFromJson.privacyPolicy)
            intent.putExtra(
                "title",
                resources.getString(com.lib.module_res.R.string.privacy_policy)
            )
            startActivity(intent)
        }
        binding.termsContainer.setOnClickListener {
            val intent = Intent(this, RongWebviewActivity::class.java)
            intent.putExtra("isFull", true)
            intent.putExtra("url", policyModelFromJson.privacyPolicy)
            intent.putExtra("title", resources.getString(com.lib.module_res.R.string.terms))
            startActivity(intent)
        }
        binding.titleBar.setTitleText(getString(com.lib.module_res.R.string.settings))
        binding.titleBar.setTitleBarPadding(StatusBarUtil.getStatusBarHeight(this))

        binding.txtSettingDeleteAccount.setOnClickListener {
            CustomPopup(this)
                .setLayoutId(R.layout.popup_delete_account)
                .setControllerListener {
                    run {
//                        it.findViewById<TextView>(R.id.tvDeleteAccountHint).text = hintContent
                    }

                }
                .setOnClickListener(R.id.tvCancel) { dialog, view ->
                    run {
                        dialog.dismiss()
                    }
                }
                .setOnClickListener(R.id.tvDelete) { dialog, view ->
                    dialog.dismiss()
                    mViewModel?.getUserDelete(object : HttpRequestListener<String>() {
                        override fun addRequestParams(params: HashMap<String, Any>) {
                        }

                        override fun success(t: String?) {
                            UserUtil.deleteAccount(this@SettingsActivity)
                        }

                    })
                }.show()
        }
    }

    override fun initData() {

    }

    companion object {

        fun start(context: Context?) {
            val intent = Intent(context, SettingsActivity::class.java)
            context?.startActivity(intent)
        }
    }
}